// 实现类
package com.fujun.cloud.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fujun.cloud.order.entity.Order;
import com.fujun.cloud.order.feign.UserFeignClient;
import com.fujun.cloud.order.mapper.OrderMapper;
import com.fujun.cloud.order.service.OrderService;
import com.fujun.cloud.user.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    //注入远程调用工具
//    @Autowired
//    private RestTemplate restTemplate;

    //替代RestTemplate,进行远程调用
    @Autowired
    private UserFeignClient restTemplate;
    @Autowired
    private UserFeignClient userFeignClient;


    @Override
    public Order findOrderByOrderId(Long orderId) {
        //步骤1:根据订单id查询订单详情
        Order order = orderMapper.selectOrderByOrderId(orderId);
        //步骤2:发起远程调用,查询用户信息
        //Host: localhost  Port: 10100
        //步骤2.2 发起远程调用
        // http://localhost:10100/api/user/findUserById/1
       /* User user = restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/api/user/findUserById/" +
                order.getUserId(), User.class);*/

//        User user = restTemplate.getForObject("http://my-cloud-user/api/user/findUserById/" +
//                order.getUserId(), User.class);

        User user = userFeignClient.findUserById(order.getUserId());
        //步骤3:将用户信息设置到订单对象中
        order.setUser(user);
        //步骤4:返回订单对象
        return order;
    }


}